package com.module.lemlin.owner

import androidx.lifecycle.ViewModelProvider
import androidx.lifecycle.ViewModelStoreOwner

abstract class OwnerViewModeFragment<ViewModel : OwnerViewModel> : OwnerAbstractFragment() {

    lateinit var viewModel: ViewModel

    abstract fun viewModel(): ViewModel

    override fun doBeforeInit() {
        super.doBeforeInit()
        viewModel = viewModel()
        initViewMode()
    }

    /**
     * 初始化 view mode
     * */
    open fun initViewMode() = Unit

    /**
     * 生成ViewModel的方法
     */
    inline fun <reified VM : OwnerViewModel> createViewModel(
        owner: ViewModelStoreOwner = this
    ): VM = ViewModelProvider(
        owner, ViewModelProvider.AndroidViewModelFactory(OwnerApplication.application)
    ).get(VM::class.java)

}